package com.tjetc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.EmployeeMapper;
import com.tjetc.dao.GoodsInfoMapper;
import com.tjetc.dao.PostInfoMapper;
import com.tjetc.entity.*;
import com.tjetc.service.GoodsInfoService;
import com.tjetc.service.PostInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public JsonResult findById(Long id) {
        //selectById是mybatisplus提供的单表查询操作，生成sql语句都是mybatisplus内置
        GoodsInfo goodsInfo=goodsInfoMapper.selectById(id);
        Employee employee=employeeMapper.selectById(goodsInfo.getManagerId());
        goodsInfo.setManager(employee);
        return JsonResult.success(goodsInfo);
    }
    @Override
    public JsonResult findPage(String name, Integer pageNo, Integer pageSize) {
        //设置页码和每页数量
        Page<GoodsInfo> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<GoodsInfo>goodsInfoPage= goodsInfoMapper.selectPageLikeName(page,name);
        for (GoodsInfo goodsInfo : goodsInfoPage.getRecords()) {
            Employee employee=employeeMapper.selectById(goodsInfo.getManagerId());
            goodsInfo.setManager(employee);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(goodsInfoPage);
    }
    @Override
    public JsonResult deleteById(Long id) {
        int rows=goodsInfoMapper.deleteById(id);
        if (rows>0){
            return JsonResult.success("删除成功");
        }
        else {
            return JsonResult.fail("删除失败");
        }
    }
    //@Transactional用在service方法上，保证事务
    //注意：默认情况@Transactional只对运行时异常起作用(RuntimeException)
    @Transactional
    @Override
    public JsonResult save(GoodsInfo goodsInfoParam) {
        GoodsInfo goodsInfo=null;
        LocalDateTime now=LocalDateTime.now();
        //通过判断id值有无判断是新增还是更新，id值无是新增，id值有就是更新
        if(goodsInfoParam.getMaterialId()==null||goodsInfoParam.getMaterialId()<=0){
            //新增
            goodsInfo=new GoodsInfo();
            BeanUtils.copyProperties(goodsInfoParam,goodsInfo);
            goodsInfo.setCreateTime(now);
            goodsInfo.setUpdateTime(now);
            goodsInfoMapper.insert(goodsInfo);
            return JsonResult.success("新增成功",null);
        }else{
            //更新
            //根据id查询Admin信息
            goodsInfo=goodsInfoMapper.selectById(goodsInfoParam.getMaterialId());
            if(goodsInfo==null){
                return JsonResult.fail("用户不存在，更新失败");
            }
            //要更新的字段
            goodsInfo.setName(goodsInfoParam.getName());
            goodsInfo.setUpdateTime(now);
            goodsInfo.setManager(goodsInfoParam.getManager());
            goodsInfo.setIsDeleted(goodsInfoParam.getIsDeleted());
            goodsInfo.setExpireDate(goodsInfoParam.getExpireDate());
            goodsInfo.setLastRepairDate(goodsInfoParam.getLastRepairDate());
            goodsInfo.setLocation(goodsInfoParam.getLocation());
            goodsInfo.setManagerId(goodsInfoParam.getManagerId());
            goodsInfo.setMinQuantity(goodsInfoParam.getMinQuantity());
            goodsInfo.setPurchaseDate(goodsInfoParam.getPurchaseDate());
            goodsInfo.setPurchasePrice(goodsInfoParam.getPurchasePrice());
            goodsInfo.setQuantity(goodsInfoParam.getQuantity());
            goodsInfo.setRepairCount(goodsInfoParam.getRepairCount());
            goodsInfo.setStatus(goodsInfoParam.getStatus());
            goodsInfo.setSupplier(goodsInfoParam.getSupplier());
            goodsInfo.setTotalPrice(goodsInfoParam.getTotalPrice());
            goodsInfo.setType(goodsInfoParam.getType());
            goodsInfo.setWarrantyPeriod(goodsInfoParam.getWarrantyPeriod());
            int rows=goodsInfoMapper.updateById(goodsInfo);
            if (rows>0){
                return JsonResult.success("更新成功",null);
            }else {
                return JsonResult.fail("用户不存在，更新失败");
            }
        }
    }
}
